﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using DataAccessLayer;
using System.Data;
using Common;


namespace VMS_Web
{
    public partial class UnitCodeMaster : System.Web.UI.Page
    {
        #region Intial declaration

        protected MainMaster CurrentMasterPage = null;
        private Master_UnitCode_Types_DB ObjMaster_UnitCode_Types_DB = null;


        #endregion

        #region Page Events

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            CurrentMasterPage = (MainMaster)this.Master;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                ListItem item = new ListItem("--Select--", "");
                ddlrefunitcode.Items.Insert(0, item);
                if (Request["Mode"] == "Edit" && Request["Type"] != null)
                {
                    bindform(Request["Type"].ToString());
                    btnAction.InnerText = "Edit";
                }

                else if (Request["Mode"] == "Delete" && Request["Type"] != null)
                {
                    bindform(Request["Type"].ToString());
                    btnAction.InnerText = "Delete";
                }
                else
                {
                    btnAction.InnerText = "Add";
                }
                if (Request["Mode"] == null)
                {
                    btnAction.InnerText = "Add";
                }
            }

            bindgrid();
        }

        #endregion


        #region button and check box  events

        protected void btnCancel_ServerClick(object sender, EventArgs e)
        {
            txtunitcode.Value = "";
            txtunitcodedesc.Value = "";
            txtminhours.Value = "";
            txtmaxhours.Value = "";
            chkcustomunit.Checked = false;
            ddlrefunitcode.Enabled = false;
            ddlrefunitcode.SelectedIndex = 0;
            btnAction.InnerText = "Add";


        }

        protected void btnAction_ServerClick(object sender, EventArgs e)
        {

            try
            {

                if (ObjMaster_UnitCode_Types_DB == null)
                {
                    ObjMaster_UnitCode_Types_DB = new Master_UnitCode_Types_DB();
                }


                //To Add new master type
                //For temparaty purpose user_id parameter is given as JitenP. Kindly replace with Session User_ID once login stuff is implemented
                if (Request["Mode"] == null)
                {
                    string restult_message;

                    MethodResult restult = ObjMaster_UnitCode_Types_DB.AddUnitCodeMaster(txtunitcode.Value, txtunitcodedesc.Value, chkcustomunit.Checked, ddlrefunitcode.SelectedValue, Convert.ToInt16(txtminhours.Value), Convert.ToInt16(txtmaxhours.Value), "true", "Chandra", out restult_message);


                    CurrentMasterPage.MessageToDisplay(restult);

                }

                //To Edit existing master type

                if (Request["Mode"] == "Edit")
                {
                    string restult_message;

                    MethodResult restult = ObjMaster_UnitCode_Types_DB.UpdateUnitCodeMaster(txtunitcode.Value, txtunitcodedesc.Value, chkcustomunit.Checked, ddlrefunitcode.SelectedValue, Convert.ToInt16(txtminhours.Value), Convert.ToInt16(txtmaxhours.Value), "true", "Chandra", out restult_message);

                    CurrentMasterPage.MessageToDisplay(restult);

                }

                // To Cancel Master Unit code
                if (Request["Mode"] == "Delete")
                {
                    string restult_message;

                    MethodResult restult = ObjMaster_UnitCode_Types_DB.DeleteUnitCodeMaster(txtunitcode.Value, "Chandra", out restult_message);

                    CurrentMasterPage.MessageToDisplay(restult);

                }




                btnCancel_ServerClick(null, null);

            }

            catch (Exception exp)
            {

                CurrentMasterPage.MessageToDisplay(MessageType.Error, "Oops!Some thing went wrong, contact application support team");

            }


        }

        protected void chkcustomunit_CheckedChanged(object sender, EventArgs e)
        {
            if (chkcustomunit.Checked == true)
            {
                ddlrefunitcode.Enabled = true;
                binddropdown();
            }
            else
            {
                ddlrefunitcode.Enabled = false;
                ListItem item = new ListItem("--Select--", "");
                ddlrefunitcode.Items.Insert(0, item);
            }

        }

        #endregion

        #region Custom Functions
        private void bindgrid()
        {
            if (ObjMaster_UnitCode_Types_DB == null)
            {
                ObjMaster_UnitCode_Types_DB = new Master_UnitCode_Types_DB();
            }
            grdunitcodemasterdetails.DataSource = ObjMaster_UnitCode_Types_DB.GetUnitCodeMasterItems("", "AL");
            grdunitcodemasterdetails.DataBind();

        }

        private void bindform(string unit_code)
        {
            if (ObjMaster_UnitCode_Types_DB == null)
            {
                ObjMaster_UnitCode_Types_DB = new Master_UnitCode_Types_DB();
            }
            List<Master_UnitCode_Types> _list = ObjMaster_UnitCode_Types_DB.GetUnitCodeMasterItems(unit_code, "SL");

            if (_list.Count != 0)
            {
                txtunitcode.Value = _list[0].Unit_Code.ToString();
                txtunitcodedesc.Value = _list[0].Desc.ToString();
                chkcustomunit.Checked = Convert.ToBoolean(_list[0].IsCustomUnit.ToString());
                if (Convert.ToBoolean(_list[0].IsCustomUnit.ToString()) == true)
                {
                    binddropdown();
                    ddlrefunitcode.Enabled = true;
                    ddlrefunitcode.Items.FindByValue(_list[0].Ref_UnitCode.ToString()).Selected = true;

                }
                else
                {
                    ddlrefunitcode.Enabled = false;
                }

                txtminhours.Value = _list[0].Min_Hours.ToString();
                txtmaxhours.Value = _list[0].Max_Hours.ToString();

            }

        }

        public string RedirectForEdit(string type)
        {
            return "UnitCodeMaster.aspx?Mode=Edit&Type=" + type;

        }
        public string RedirectForDelete(string type)
        {
            return "UnitCodeMaster.aspx?Mode=Delete&Type=" + type;

        }

        public void binddropdown()
        {

            if (ObjMaster_UnitCode_Types_DB == null)
            {
                ObjMaster_UnitCode_Types_DB = new Master_UnitCode_Types_DB();
            }
            List<Master_UnitCode_Types> _list = ObjMaster_UnitCode_Types_DB.GetUnitCodeMasterItems("", "AL");

            if (_list.Count != 0)
            {
                ListItem item = new ListItem("--Select--", "");
                ddlrefunitcode.DataSource = _list;
                ddlrefunitcode.DataTextField = "Unit_Code";
                ddlrefunitcode.DataValueField = "Unit_Code";
                ddlrefunitcode.DataBind();
                ddlrefunitcode.Items.Insert(0, item);


            }







        }
        #endregion


        protected void grdunitcodemasterdetails_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdunitcodemasterdetails.CurrentPageIndex = e.NewPageIndex;
            bindgrid();

        }

        public void clearquerystring()
        {
            PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            // make collection editable
            isreadonly.SetValue(this.Request.QueryString, false, null);
            // remove
            this.Request.QueryString.Clear();
        }
    }
}